/*
 * AgentUtils.cpp
 *
 *  Created on: Jan 26, 2009
 *      Author: Andrzej Olchawa
 *      mailto: andrzejolchawa@gmail.com
 *      http://www.hardtechnology.org
 *      This is a part of the Armazd project, which is covered by
 *      GNU GPL v2 license.
 */

#include "AgentUtils.h"

AgentUtils::AgentUtils() {}

AgentUtils::~AgentUtils() {}

int AgentUtils::extractSensorInfoFromConfigLine(string &path, int &delay,
		const string &configLine, const int &lineIndex)
{
	string line = configLine;

	int lIdx = line.find('\"',0);
	int rIdx = line.find('\"',lIdx+1);

	if (lIdx < rIdx)
	{
		string trimSet = string(WHITE_SPACES) + "\"";
		path = HTStringUtilities::trim(line.substr(lIdx, rIdx-lIdx), trimSet);

		lIdx = line.find('\"', rIdx+1);
		rIdx = line.find('\"', lIdx+1);
		if (lIdx < rIdx)
		{
			string trimSet = string(WHITE_SPACES) + "\"";
			delay = HTConverter::strToInt(HTStringUtilities::trim(
									line.substr(lIdx, rIdx-lIdx), trimSet));
		}
		else
		{
			throw AgentException("int AgentUtils.extractDestinationsInfoFromConfigLine: \
								  syntax error at line:" + HTConverter::intToStr(lineIndex));
		}
	}
	else
	{
		throw AgentException("int AgentUtils.extractDestinationsInfoFromConfigLine: \
							  syntax error at line:" + HTConverter::intToStr(lineIndex));
	}

	return 0;
}

int AgentUtils::extractDestinationsInfoFromConfigLine(string &host, int &port,
		const string &configLine, const int &lineIndex)
{
	string line = configLine;

	int lIdx = line.find('\"',0);
	int rIdx = line.find('\"',lIdx+1);

	if (lIdx < rIdx)
	{
		string trimSet = string(WHITE_SPACES) + "\"";
		host = HTStringUtilities::trim(line.substr(lIdx, rIdx-lIdx), trimSet);

		lIdx = line.find('\"', rIdx+1);
		rIdx = line.find('\"', lIdx+1);
		if (lIdx < rIdx)
		{
			string trimSet = string(WHITE_SPACES) + "\"";
			port = HTConverter::strToInt(HTStringUtilities::trim(
									line.substr(lIdx, rIdx-lIdx), trimSet));
		}
		else
		{
			throw AgentException("int AgentUtils.extractDestinationsInfoFromConfigLine: \
								  syntax error at line:" + HTConverter::intToStr(lineIndex));
		}
	}
	else
	{
		throw AgentException("int AgentUtils.extractDestinationsInfoFromConfigLine: \
							  syntax error at line:" + HTConverter::intToStr(lineIndex));
	}

	return 0;
}

int AgentUtils::extractListenersInfoFromConfigLine(string &path, int &port,
		const string &configLine, const int &lineIndex)
{
	string line = configLine;
	int listenerType = -1;

	int lIdx = line.find('\"',0);
	int rIdx = line.find('\"',lIdx+1);

	if (lIdx < rIdx)
	{
		string trimSet = string(WHITE_SPACES) + "\"";
		listenerType = getListenerType(HTStringUtilities::trim(line.substr(lIdx, rIdx-lIdx), trimSet));

		lIdx = line.find('\"', rIdx+1);
		rIdx = line.find('\"', lIdx+1);
		if (lIdx < rIdx)
		{
			string trimSet = string(WHITE_SPACES) + "\"";

			switch (listenerType)
			{
				case LISTENER_FILE:
				{
					path = HTStringUtilities::trim(line.substr(lIdx, rIdx-lIdx), trimSet);
					break;
				}
				case LISTENER_PORT:
				{
					port = HTConverter::strToInt(HTStringUtilities::trim(
											line.substr(lIdx, rIdx-lIdx), trimSet));
					break;
				}
				default:
				{
					throw AgentException("int AgentUtils.extractListenersInfoFromConfigLine: \
										  unknown listener type:" + HTConverter::intToStr(lineIndex));
				}
			}
		}
		else
		{
			throw AgentException("int AgentUtils.extractDestinationsInfoFromConfigLine: \
								  syntax error at line:" + HTConverter::intToStr(lineIndex));
		}
	}
	else
	{
		throw AgentException("int AgentUtils.extractDestinationsInfoFromConfigLine: \
							  syntax error at line:" + HTConverter::intToStr(lineIndex));
	}

	return listenerType;
}

int AgentUtils::getListenerType(string typeName)
{
	if (typeName.compare(LISTENER_FILE_NAME) == 0)
		return LISTENER_FILE;
	else if (typeName.compare(LISTENER_PORT_NAME) == 0)
		return LISTENER_PORT;
	else
		return -1;
}

string AgentUtils::getListenerType(int type)
{
	switch (type)
	{
		case LISTENER_FILE: return LISTENER_FILE_NAME;
		case LISTENER_PORT: return LISTENER_PORT_NAME;
		default: return "";
	}
}
